package com.mongoosehome.web.util;

import com.mongoosehome.web.enumcommon.PageSizeEnum;
import java.util.ArrayList;
import java.util.List;

/**
 * 分页对象，包含数据及分页信息
 *
 */
public final class PageInfo {
    // 每页的默认记录数
    
    private int pageSize = PageSizeEnum.PAGE_SIZE.getValue();    // 每页的记录数
    private int currentPage;                    //当前页
    private int totalPage;                      //总页数
    private List<?> fileList;                   //当前页中存放的对象集
    private int totalCount;                     //文件总记录数
    private int firstNo;                        //当前页数中的第一条在数据库中的位置。

    /**
     * 默认构造函数，只构造空页
     */
    public PageInfo() {
        this.totalCount = 0;
        this.fileList = new ArrayList<Object>();
    }

    /**
     * 使用指定的信息创建一个页面信息
     *
     * @param totalCount 数据库中总记录条数
     * @param pageSize 本页容量
     * @param currentPage 
     * @param fileList 本页包含的对象集
     */
    public PageInfo(int currentPage, int totalCount, int pageSize,List<?> fileList) {
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.fileList = fileList;
        this.currentPage = currentPage;       
    }

    /**
     * 
     * @param currentPage
     * @param totalCount
     * @param pageSize 
     */
    public PageInfo(int currentPage, int totalCount, int pageSize) {
        //初始化pageInfo数据。       
        this.setPageSize(pageSize);
        this.totalCount = totalCount;
        this.setTotalPage(this.getTotalPage());
        this.setCurrentPage(currentPage);
    }
    
    /**
     * @return the currentPage
     */
    public int getCurrentPage() {
        if (currentPage<1) return 1;
        if (currentPage>this.totalPage) return this.totalPage;
        return currentPage;
    }

    /**
     * @param currentPage the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage=currentPage<1?1:currentPage>this.totalPage?this.totalPage:currentPage;
    }

    /**
     * @return the totalPage
     */
    public int getTotalPage() {
        if (pageSize!=0){
            this.totalPage  =  (this.totalCount+this.pageSize-1)/this.pageSize;
        }        
        return totalPage;
    }

    /**
     * @param totalPage the totalPage to set
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    
        /**
     * @return the fileList
     */
    public List<?> getFileList() {
        return fileList;
    }

    /**
     * @param fileList the fileList to set
     */
    public void setFileList(List<?> fileList) {
        this.fileList = fileList;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        //只有值有效才修改。
        if(pageSize>0)  this.pageSize = pageSize;
    }
    
     /**
     * @return the totalCount
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * @param totalCount the totalCount to set
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * @return the firstNo
     */
    public int getFirstNo() {
        this.firstNo = this.pageSize*(this.currentPage-1);
        return this.firstNo;
    }

    /**
     * @param firstNo the firstNo to set
     */
    public void setFirstNo(int firstNo) {
        this.firstNo = firstNo;
    }
}
